home *** CD-ROM | disk | FTP | other *** search
/ Giga Games 1 / Giga Games.iso / net / usenet / volume1 / sol < prev    next >
Encoding:
Internet Message Format  |  1987-07-21  |  36.2 KB

  1. Path: uunet!seismo!rochester!cornell!uw-beaver!tektronix!tekgen!tekred!games-request
  2. From: games-request@tekred.TEK.COM
  3. Newsgroups: comp.sources.games
  4. Subject: v01i098:  sol - solitaire game (2 versions)
  5. Message-ID: <1413@tekred.TEK.COM>
  6. Date: 20 Jul 87 21:59:52 GMT
  7. Sender: billr@tekred.TEK.COM
  8. Lines: 1749
  9. Approved: billr@tekred.TEK.COM
  10.  
  11. Submitted by: David Goodenough <dg@wrs.UUCP>
  12. Comp.sources.games: Volume 1, Issue 98
  13. Archive-name: sol
  14.  
  15.     [Just type "make" then run it.  Typing 'h <RETURN>' in response
  16.      to cmd> gives displays help screen.    -br]
  17.  
  18. #! /bin/sh
  19. # This is a shell archive.  Remove anything before this line, then unpack
  20. # it by saving it into a file and typing "sh file".  To overwrite existing
  21. # files, type "sh file -c".  You can also feed this as standard input via
  22. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  23. # will see the following message at the end:
  24. #        "End of shell archive."
  25. # Contents:  README Makefile sol.c solx.c
  26. # Wrapped by billr@tekred on Mon Jul 20 14:55:47 1987
  27. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  28. if test -f README -a "${1}" != "-c" ; then 
  29.   echo shar: Will not over-write existing file \"README\"
  30. else
  31. echo shar: Extracting \"README\" \(1341 characters\)
  32. sed "s/^X//" >README <<'END_OF_README'
  33. XSources for two solitaire games - a while back someone posted some sources
  34. Xin some language or other that played solitaire. With about two evenings
  35. Xwoth of work that became sol.c - standard solitaire. To compile either game
  36. X(under BSD4.3) use a command like 'cc -o sol sol.c -ltermcap' or
  37. X'cc -o solx solx.c -ltermcap' - you'll have to adjust on Xenix for termcap
  38. Xetc. etc. etc. solx.c is a different solitaire game, and a lot more
  39. Xdifficult to get out. You start with 1 run containing a single card
  40. X(#1 in the game), then hidden piles 2 thru 7, containing 2 thru 7 cards
  41. Xrespectively. The remaining 24 cards are dealt on runs 2 thru 7 - four
  42. Xin each run. There is no deck - all you can do is to move runs around.
  43. XUnlike regular solitaire, runs can be split, hence the altered move
  44. Xcommand, and since runs can get a bit long (I've seen them as big as
  45. X22-23 cards) I've turned the display on it's side. Both games have several
  46. Xcheats, and an autopilot, although the autopilot for solx is not very clever.
  47. XIn order to prevent thrashing of runs I coded it so that it will only move
  48. Xa run if the top card being moved was not already placed. However there
  49. Xare times when such an apparently redundant move is necessary to get out.
  50. X
  51. XBest of luck, and have fun.
  52. X--
  53. X        dg@wrs.UUCP - David Goodenough
  54. X
  55. X                    +---+
  56. X                    | +-+-+
  57. X                    +-+-+ |
  58. X                      +---+
  59. END_OF_README
  60. if test 1341 -ne `wc -c <README`; then
  61.     echo shar: \"README\" unpacked with wrong size!
  62. fi
  63. # end of overwriting check
  64. fi
  65. if test -f Makefile -a "${1}" != "-c" ; then 
  66.   echo shar: Will not over-write existing file \"Makefile\"
  67. else
  68. echo shar: Extracting \"Makefile\" \(176 characters\)
  69. sed "s/^X//" >Makefile <<'END_OF_Makefile'
  70. X# Makefile for soliaire games
  71. XCFLAGS = -O
  72. X
  73. XGAMES = sol solx
  74. X
  75. Xall:    $(GAMES)
  76. Xsol:    sol.c
  77. X    cc $(CFLAGS) -o sol sol.c -ltermcap
  78. X
  79. Xsolx:    solx.c
  80. X    cc $(CFLAGS) -o solx solx.c -ltermcap
  81. END_OF_Makefile
  82. if test 176 -ne `wc -c <Makefile`; then
  83.     echo shar: \"Makefile\" unpacked with wrong size!
  84. fi
  85. # end of overwriting check
  86. fi
  87. if test -f sol.c -a "${1}" != "-c" ; then 
  88.   echo shar: Will not over-write existing file \"sol.c\"
  89. else
  90. echo shar: Extracting \"sol.c\" \(16609 characters\)
  91. sed "s/^X//" >sol.c <<'END_OF_sol.c'
  92. X#include    <stdio.h>
  93. X#include    <signal.h>
  94. X#include    <sys/time.h>
  95. X#include    <sys/types.h>
  96. X#include    <sys/stat.h>
  97. X#include    <sys/ioctl.h>
  98. X
  99. X#define     TRUE    1
  100. X#define        FALSE    0
  101. X
  102. Xint    amode;
  103. Xint    cnt;
  104. Xint    cheat;
  105. Xint    deck[53];
  106. Xint    over[53];
  107. Xint    ace[4][14];
  108. Xint    run[7][14];
  109. Xint    hidden[7][8];
  110. Xchar    cmd[40], *cp;
  111. Xchar    *resgb;
  112. X
  113. Xchar    PC;
  114. Xchar    BC[2];
  115. Xchar    UP[2];
  116. X
  117. Xchar    *sc_bs;
  118. Xchar    *sc_move;
  119. Xchar    *sc_clear;
  120. Xchar    *sc_cleol;
  121. Xchar    *sc_init;
  122. Xchar    *sc_deinit;
  123. Xchar    *sc_s_in;
  124. Xchar    *sc_s_out;
  125. X
  126. Xshort    ospeed;
  127. X
  128. Xmain(n, a)
  129. Xchar **a;
  130. X {
  131. X    int i, p, c, r, s;
  132. X    struct timeval tp;
  133. X    struct timezone tzp;
  134. X    int from;
  135. X    int dest;
  136. X    int brkflg;
  137. X
  138. X    amode = 0;
  139. X    gettimeofday(&tp, &tzp);
  140. X    srand(getpid() + tp.tv_sec);
  141. X    rawmode(TRUE);
  142. X    setsig(TRUE);
  143. X    getterm();
  144. X    init();
  145. X    for (i = 0; i < 52; i++)
  146. X      deck[i] = i;
  147. X    deck[52] = -1;
  148. X    shuffle(deck);
  149. X    for (i = 0; i < 4; i++)
  150. X      ace[i][0] = -1;
  151. X    for (i = 0; i < 7; i++)
  152. X      hidden[i][0] = run[i][0] = -1;
  153. X    over[0] = -1;
  154. X    for (p = 0; p < 7; p++)
  155. X      for (c = p; c < 7; c++)
  156. X    put(hidden[c],get(deck));
  157. X    for (r = 0; r < 7; r++)
  158. X      put(run[r],get(hidden[r]));
  159. X    redraw(TRUE);
  160. X    for (r = 0; r < 7; r++)
  161. X     {
  162. X    while (getvalue(run[r][0]) == 0)
  163. X     {
  164. X        s = getsuit(run[r][0]);
  165. X        put(ace[s],get(run[r]));
  166. X        put(run[r],get(hidden[r]));
  167. X        redraw(FALSE);
  168. X     }
  169. X     }
  170. X    cnt = cheat = 0;
  171. X    if (n > 1 && !strcmp(a[1], "auto"))
  172. X      autopilot();
  173. X    brkflg = 1;
  174. X    while (brkflg)
  175. X     {
  176. X    cnt++;
  177. X    do
  178. X     {
  179. X        moveto(17, 0);
  180. X        printf("cmd:%d> ", cnt);
  181. X        cleol();
  182. X        fflush(stdout);
  183. X     } while (!getst(cp = cmd));
  184. X    while (*cp == ' ' || *cp == '\t')
  185. X      cp++;
  186. X    switch (*cp++)
  187. X     {
  188. X        case 'a':
  189. X          autopilot();
  190. X        break;
  191. X        case 'm':
  192. X         {
  193. X        while (*cp == ' ' || *cp == '\t')
  194. X          cp++;
  195. X        if (!(from = *cp++) || ((from < '1' || from > '7') &&
  196. X                                from != 'd'))
  197. X         {
  198. X            whoops();
  199. X            continue;
  200. X         }
  201. X        while (*cp == ' ' || *cp == '\t')
  202. X          cp++;
  203. X        if (!(dest = *cp++) || ((dest < '1' || dest > '7') &&
  204. X                dest != 'a') || !movecard(from, dest, FALSE))
  205. X         {
  206. X            whoops();
  207. X            continue;
  208. X         }
  209. X        if (cardsleft() == 0)
  210. X          finish();
  211. X         }
  212. X        break;
  213. X        case 't':
  214. X        case 0:
  215. X          thumb();
  216. X        break;
  217. X        case 'h':
  218. X        case '?':
  219. X          disphelp();
  220. X        break;
  221. X        case 'r':
  222. X          disprules();
  223. X        break;
  224. X        case 'q':
  225. X          brkflg = 0;
  226. X        break;
  227. X        case 's':
  228. X         {
  229. X        while (peek(over) != -1)
  230. X          put(deck, get(over));
  231. X        shuffle(deck);
  232. X        redraw(FALSE);
  233. X        cheat++;
  234. X         }
  235. X        break;
  236. X        case 'p':
  237. X         {
  238. X        while (*cp == ' ' || *cp == '\t')
  239. X          cp++;
  240. X        if (!(from = *cp++) || from < '1' || from > '7')
  241. X         {
  242. X            whoops();
  243. X            continue;
  244. X         }
  245. X            if (hidden[from -= '1'][0] != -1)
  246. X         {
  247. X            while (hidden[from][0] != -1)
  248. X              put(deck, get(hidden[from]));
  249. X            redraw(FALSE);
  250. X            cheat++;
  251. X         }
  252. X        else
  253. X          whoops();
  254. X         }
  255. X        break;
  256. X        case 'd':
  257. X         {
  258. X        moveto(19, 0);
  259. X        printf("%d cards in deck:\n", lstlen(deck) + lstlen(over));
  260. X        i = lstlen(deck);
  261. X        while (--i != -1)
  262. X         {
  263. X            pcard(deck[i]);
  264. X            putchar(' ');
  265. X         }
  266. X        putchar('\n');
  267. X        for (i = 0; over[i] != -1; i++)
  268. X         {
  269. X            pcard(over[i]);
  270. X            putchar(' ');
  271. X         }
  272. X        printf("\nHit return -");
  273. X        fflush(stdout);
  274. X        getcx();
  275. X        moveto(19, 0);
  276. X        for (i = 0; i < 4; i++)
  277. X         {
  278. X            cleol();
  279. X            putchar('\n');
  280. X         }
  281. X        cheat++;
  282. X         }
  283. X        break;
  284. X        case 'w':
  285. X         {
  286. X        while (*cp == ' ' || *cp == '\t')
  287. X          cp++;
  288. X        if (!(from = *cp++) || from < '1' || from > '7')
  289. X         {
  290. X            whoops();
  291. X            continue;
  292. X         }
  293. X        moveto(19, 0);
  294. X        from -= '1';
  295. X        printf("%d cards hidden under run %c:\n", lstlen(hidden[from]),
  296. X                                from + '1');
  297. X        for (i = 0; hidden[from][i] != -1; i++)
  298. X         {
  299. X            pcard(hidden[from][i]);
  300. X            putchar(' ');
  301. X         }
  302. X        printf("\nHit return -");
  303. X        fflush(stdout);
  304. X        getcx();
  305. X        moveto(19,0);
  306. X        for (i = 0; i < 4; i++)
  307. X         {
  308. X            cleol();
  309. X            putchar('\n');
  310. X         }
  311. X        cheat++;
  312. X         }
  313. X        break;
  314. X        default:
  315. X          whoops();
  316. X        break;
  317. X     }
  318. X     }
  319. X    moveto(19, 0);
  320. X    cleol();
  321. X    printf("I see you gave up\n");
  322. X    if (cheat > 0)
  323. X      printf("    ... even after you cheated %d times!\n", cheat);
  324. X    else
  325. X      printf("    ... but at least you didn't cheat...congratulations!\n");
  326. X    unwind();
  327. X }
  328. X
  329. Xunwind()
  330. X {
  331. X    moveto(23, 0);
  332. X    deinit();
  333. X    rawmode(FALSE);
  334. X    exit(0);
  335. X }
  336. X
  337. Xmovecard(from,dest,limitmove)
  338. X {
  339. X    if (from == dest)
  340. X      return(FALSE);
  341. X    if (from == 'd')
  342. X      return(dest == 'a' ? deck2ace() : deck2run(dest));
  343. X    else
  344. X      return(dest == 'a' ? run2ace(from) : run2run(from,dest,limitmove));
  345. X }
  346. X
  347. Xdeck2run(dest)
  348. X {
  349. X    int fcard, dcard, s, ok;
  350. X
  351. X    if ((fcard = peek(over)) == -1)
  352. X      return(FALSE);
  353. X    dcard = peek(run[dest -= '1']);
  354. X    if (ok = chk2run(fcard,dcard))
  355. X     {
  356. X    put(run[dest],get(over));
  357. X    redraw(FALSE);
  358. X    while (getvalue(peek(over)) == 0)
  359. X     {
  360. X        s = getsuit(peek(over));
  361. X        put(ace[s],get(over));
  362. X        redraw(FALSE);
  363. X     }
  364. X     }
  365. X    return(ok);
  366. X }
  367. X
  368. Xdeck2ace()
  369. X {
  370. X    int fcard, s, ok;
  371. X
  372. X    if (ok = (fcard = peek(over)) != -1 &&
  373. X        getvalue(peek(ace[s = getsuit(fcard)])) == getvalue(fcard) - 1)
  374. X     {
  375. X    put(ace[s], get(over));
  376. X    redraw(FALSE);
  377. X    while (getvalue(peek(over)) == 0)
  378. X     {
  379. X        s = getsuit(peek(over));
  380. X        put(ace[s],get(over));
  381. X        redraw(FALSE);
  382. X     }
  383. X     }
  384. X    return(ok);
  385. X }
  386. X
  387. Xrun2ace(from)
  388. X {
  389. X    int fcard, s, ok;
  390. X
  391. X    fcard = peek(run[from -= '1']);
  392. X    if (ok = getvalue(peek(ace[s = getsuit(fcard)])) == getvalue(fcard) - 1)
  393. X     {
  394. X    put(ace[s], get(run[from]));
  395. X    redraw(FALSE);
  396. X    if (run[from][0] == -1 && hidden[from][0] >= 0)
  397. X     {
  398. X        while (getvalue(peek(hidden[from])) == 0)
  399. X         {
  400. X        s = getsuit(peek(hidden[from]));
  401. X        put(ace[s],get(hidden[from]));
  402. X        redraw(FALSE);
  403. X         }
  404. X        put(run[from],get(hidden[from]));
  405. X        redraw(FALSE);
  406. X     }
  407. X     }
  408. X    return(ok);
  409. X }
  410. X
  411. Xrun2run(from,dest,limitmove)
  412. X {
  413. X    int fcard, dcard, i, ok, s;
  414. X
  415. X    if ((fcard = run[from -= '1'][0]) == -1)
  416. X      return(FALSE);
  417. X    dcard = peek(run[dest -= '1']);
  418. X    if (amode > 0 && dcard == -1 && getvalue(fcard) == 12 &&
  419. X                        lstlen(hidden[from]) == 0)
  420. X      return(FALSE);
  421. X    if (amode > 0 && !limitmove && lstlen(hidden[from]) == 0)
  422. X      return(FALSE);
  423. X    if (ok = chk2run(fcard,dcard))
  424. X     {
  425. X    for (i = 0; run[from][i] != -1; i++)
  426. X      put(run[dest], run[from][i]);
  427. X    run[from][0] = -1;
  428. X    redraw(FALSE);
  429. X    if (lstlen(hidden[from]) > 0)
  430. X     {
  431. X        while (getvalue(peek(hidden[from])) == 0)
  432. X         {
  433. X        s = getsuit(peek(hidden[from]));
  434. X        put(ace[s],get(hidden[from]));
  435. X        redraw(FALSE);
  436. X         }
  437. X        put(run[from],get(hidden[from]));
  438. X        redraw(FALSE);
  439. X     }
  440. X     }
  441. X    return(ok);
  442. X }
  443. X
  444. Xchk2run(fcard,dcard)
  445. X {
  446. X    if (dcard == -1 && getvalue(fcard) == 12)
  447. X      return(TRUE);
  448. X    return(getvalue(dcard) - 1 == getvalue(fcard) &&
  449. X                getcolour(dcard) != getcolour(fcard));
  450. X }
  451. X
  452. Xfinish()
  453. X {
  454. X    int i, k;
  455. X
  456. X    moveto(19,0);
  457. X    printf("\007I'll finish for you now\007");
  458. X    fflush(stdout);
  459. X    do
  460. X     {
  461. X    k = FALSE;
  462. X    for (i = 0; i < 7; i++)
  463. X      k = movecard(i + '1', 'a', FALSE) || k;
  464. X     } while (k);
  465. X    moveto(20,0);
  466. X    cleol();
  467. X    printf("\007You WIN\007\n");
  468. X    if (cheat > 0)
  469. X      printf("    ... but you cheated %d times!", cheat);
  470. X    else
  471. X      printf("    ... and without cheating ... congratulations!");
  472. X    unwind();
  473. X }
  474. X
  475. Xautopilot()
  476. X {
  477. X    int i, j, k;
  478. X    int iter;
  479. X
  480. X    moveto(19,0);
  481. X    printf("Going into automatic mode...");
  482. X    fflush(stdout);
  483. X    amode = TRUE;
  484. X    while (cardsleft() > 0)
  485. X     {
  486. X    k = FALSE;
  487. X    for (i = 0; i < 7; i++)
  488. X      for (j = 0; j < 7; j++)
  489. X        k = movecard(i + '1', j + '1', TRUE) || k;
  490. X    for (i = 0; i < 7; i++)
  491. X     {
  492. X        k = movecard(i + '1', 'a', FALSE) || k;
  493. X        k = movecard('d', i + '1', FALSE) || k;
  494. X     }
  495. X    k = movecard('d', 'a', FALSE) || k;
  496. X    if (!k)
  497. X     {
  498. X        if (iter--)
  499. X          thumb();
  500. X        else
  501. X          break;
  502. X     }
  503. X    else
  504. X      iter = lstlen(over) + lstlen(deck);
  505. X     }
  506. X    do
  507. X     {
  508. X    k = FALSE;
  509. X    for (i = 0; i < 7; i++)
  510. X      k = movecard(i + '1', 'a', FALSE) || k;
  511. X     } while (k);
  512. X    moveto(19, 28);
  513. X    if (cardsleft() == 0)
  514. X     {
  515. X    printf("\007YEA...\007");
  516. X    fflush(stdout);
  517. X    for (i = 0; i < 7; i++)
  518. X      if (movecard(i + '1', 'a', FALSE));
  519. X    moveto(19, 34);
  520. X    printf("I won!!!!!");
  521. X     }
  522. X    else
  523. X     {
  524. X    printf("I couldn't win this time");
  525. X    moveto(20, 0);
  526. X    printf("%d cards in deck", lstlen(deck) + lstlen(over));
  527. X     }
  528. X    unwind();
  529. X }
  530. X
  531. Xredraw(display)
  532. X {
  533. X    static long_run[7];
  534. X    static long_hide[7];
  535. X    static long_ace[4];
  536. X    static base_run[7];
  537. X
  538. X    int i, j, k;
  539. X
  540. X    if (display)
  541. X     {
  542. X    clear();
  543. X    for (i = 0; i < 7; i++)
  544. X     {
  545. X        long_run[i] = long_hide[i] = base_run[i] = -1;
  546. X        moveto(0, 8 + i * 5);
  547. X        putchar(i + '1');
  548. X     }
  549. X    for (i = 0; i < 4; i++)
  550. X      long_ace[i] = -1;
  551. X    moveto(0,56);
  552. X    printf("ACES");
  553. X    moveto(6,56);
  554. X    printf("DECK");
  555. X     }
  556. X    for (i = 0; i < 7; i++)
  557. X     {
  558. X    if (long_hide[i] != lstlen(hidden[i]))
  559. X     {
  560. X        moveto(2, 8 + i * 5);
  561. X        putchar(lstlen(hidden[i]) ? lstlen(hidden[i]) + '0' : ' ');
  562. X        long_hide[i] = lstlen(hidden[i]);
  563. X     }
  564. X     }
  565. X    for (i = 0; i < 4; i++)
  566. X     {
  567. X    if (long_ace[i] != lstlen(ace[i]))
  568. X     {
  569. X        moveto(2, 49 + i * 5);
  570. X        if (ace[i][0] != -1)
  571. X          pcard(peek(ace[i]));
  572. X        else
  573. X          printf("--");
  574. X        long_ace[i] = lstlen(ace[i]);
  575. X     }
  576. X     }
  577. X    moveto(8, 55);
  578. X    printf(lstlen(deck) ? "##  " : "    ");
  579. X    if (lstlen(over))
  580. X      pcard(peek(over));
  581. X    else
  582. X      printf("  ");
  583. X    for (i = 0; i < 7; i++)
  584. X     {
  585. X    if (long_run[i] != lstlen(run[i]) ||
  586. X                (long_run[i] == 1 && base_run[i] != run[i][0]))
  587. X     {
  588. X        for (j = 0; run[i][j] != -1; j++)
  589. X         {
  590. X        moveto(j + 4, 7 + i * 5);
  591. X        if (run[i][j] == -1)
  592. X          printf("  ");
  593. X        else
  594. X          pcard(run[i][j]);
  595. X         }
  596. X        while (j < long_run[i])
  597. X         {
  598. X        moveto(j++ + 4, 7 + i * 5);
  599. X        printf("  ");
  600. X         }
  601. X        long_run[i] = lstlen(run[i]);
  602. X        base_run[i] = run[i][0];
  603. X     }
  604. X     }
  605. X    fflush(stdout);
  606. X    sleep(1);
  607. X }
  608. X
  609. Xthumb()
  610. X {
  611. X    int i, s;
  612. X
  613. X    if (deck[0] == -1)
  614. X     {
  615. X    if (over[0] == -1)
  616. X      return;
  617. X    while (over[0] != -1)
  618. X      put(deck, get(over));
  619. X     }
  620. X    for (i = 0; i < 3; i++)
  621. X      if (deck[0] != -1)
  622. X    put(over,get(deck));
  623. X    redraw(FALSE);
  624. X    while (getvalue(peek(over)) == 0)
  625. X     {
  626. X    s = getsuit(peek(over));
  627. X    put(ace[s], get(over));
  628. X    redraw(FALSE);
  629. X     }
  630. X    if (over[0] == -1 && deck[0] != -1)
  631. X      thumb();
  632. X }
  633. X
  634. Xpcard(card)
  635. X {
  636. X    int i;
  637. X
  638. X    if (i = getcolour(card))
  639. X      standout();
  640. X    printf("%c%c", "A23456789TJQK"[getvalue(card)], "HDSC"[getsuit(card)]);
  641. X    if (i)
  642. X      standend();
  643. X }
  644. X
  645. X
  646. Xgetvalue(card)
  647. X {
  648. X    return(card % 13);
  649. X }
  650. X
  651. Xgetsuit(card)
  652. X {
  653. X    return(card / 13);
  654. X }
  655. X
  656. Xgetcolour(card)
  657. X {
  658. X    return(card / 26);
  659. X }
  660. X
  661. Xcardsleft()
  662. X {
  663. X    int i, t;
  664. X
  665. X    t = lstlen(deck) + lstlen(over);
  666. X    for (i = 0; i < 7; i++)
  667. X      t += lstlen(hidden[i]);
  668. X    return(t);
  669. X }
  670. X
  671. Xwhoops()
  672. X {
  673. X    moveto(17,0);
  674. X    printf("\007Invalid Command: '%s'\007", cmd);
  675. X    fflush(stdout);
  676. X    sleep(4);
  677. X }
  678. X
  679. X
  680. Xdisphelp()
  681. X {
  682. X    clear();
  683. X    printf("\
  684. XCommands: t or RETURN     : thumb the deck 3 cards at a time\n\
  685. X          m [d1-7] [1-7a] : move cards or runs\n\
  686. X          a               : turn on the auto pilot (in case you get stuck)\n\
  687. X          s               : shuffle the deck (cheat!)\n\
  688. X          p [2-7]         : put a hidden pile into the deck (cheat!)\n\
  689. X          d               : print the cards in the deck (cheat!)\n\
  690. X          w [2-7]         : print the cards in a hidden pile (cheat!)\n\
  691. X          h or ?          : print this command summary\n\
  692. X          r               : print the rules of the game\n\
  693. X          q               : quit\n\n");
  694. X    printf("\
  695. XMoving:   1-7, 'd', or 'a' select the source and destination for a move.\n\
  696. X          Valid moves are from a run to a run, from the deck to a run,\n\
  697. X          from a run to an ace pile, and from the deck to an ace pile.\n\n\
  698. XCheating: Commands that allow cheating are available but they will count\n\
  699. X          against you in your next life!\n\n\nHit Return -");
  700. X    fflush(stdout);
  701. X    getcx();
  702. X    redraw(TRUE);
  703. X }
  704. X
  705. Xdisprules()
  706. X {
  707. X    clear();
  708. X    printf("\
  709. XObject:   The object of this game is to get all of the cards in each suit\n\
  710. X          in order on the proper ace pile.\n\n\
  711. XRules:    Cards are played on the ace piles in ascending order: A,2,...,K. \n\
  712. X          All aces are automatically placed in the correct aces pile as\n\
  713. X          they're found in the deck or in a pile of hidden cards.  Once a\n\
  714. X          card is placed in an ace pile it can't be removed.\n\n");
  715. X    printf("\
  716. X          Cards must be played in descending order: K,Q,..,2, on the seven\n\
  717. X          runs which are initially dealt.  They must always be played on a\n\
  718. X          card of the opposite color.  Runs must always be moved as a\n\
  719. X          whole, unless you're moving the lowest card on a run to the\n\
  720. X          correct ace pile.\n\n");
  721. X    printf("\
  722. X          Whenever a whole run is moved, the top hidden card is turned\n\
  723. X          over, thus becoming the beginning of a new run.  If there are no\n\
  724. X          hidden cards left, a space is created which can only be filled by\n\
  725. X          a king.\n\n\
  726. X          The rest of the deck is thumbed 3 cards at a time, until you spot a\n\
  727. X          valid move.  Whenever the bottom of the deck is reached, the cards\n\
  728. X          are turned over and you can continue thumbing.\n\n\nHit Return -");
  729. X    fflush(stdout);
  730. X    getcx();
  731. X    redraw(TRUE);
  732. X }
  733. X
  734. Xgetst(getbuf, pc)
  735. Xchar *getbuf;
  736. X {
  737. X    int ch;
  738. X    int nc;
  739. X
  740. X    *(resgb = getbuf) = nc = 0;
  741. X    for (;;)
  742. X     {
  743. X    if ((((ch = getcx()) >= ' ' && ch <= '~') || ch == '\t') && nc < 40)
  744. X     {
  745. X        *getbuf++ = ch;
  746. X        *getbuf = 0;
  747. X        nc++;
  748. X        putchar(ch);
  749. X        fflush(stdout);
  750. X     }
  751. X    else if (ch == '\b')
  752. X     {
  753. X        if (nc--)
  754. X         {
  755. X            *--getbuf = 0;
  756. X        printf("\b \b");
  757. X        fflush(stdout);
  758. X         }
  759. X        else
  760. X          return(FALSE);
  761. X     }
  762. X    else if (ch == 'r' & 0x1f)
  763. X      redraw(TRUE);
  764. X    else if (ch == '\n')
  765. X      return(TRUE);
  766. X     }
  767. X }
  768. X
  769. Xshuffle(cards)
  770. Xint *cards;
  771. X {
  772. X    int i, j, k, t;
  773. X
  774. X    for (i = 0; cards[i] != -1; i++)
  775. X      ;
  776. X    if (i)
  777. X     {
  778. X    for (j = 0; j < i; j++)
  779. X     {
  780. X        do
  781. X          k = rnd(i);
  782. X         while (k == j);
  783. X        t = cards[j];
  784. X        cards[j] = cards[k];
  785. X        cards[k] = t;
  786. X     }
  787. X     }
  788. X }
  789. X
  790. Xrnd(n)
  791. X {
  792. X    return(((rand() >> 12 | rand() >> 24) & (unsigned) ~0 >> 1) % n);
  793. X }
  794. X
  795. Xlstlen(list)
  796. Xint *list;
  797. X {
  798. X    int i;
  799. X
  800. X    for (i = 0; list[i] != -1; i++)
  801. X      ;
  802. X    return(i);
  803. X }
  804. X
  805. Xrestart()
  806. X {
  807. X    setsig(TRUE);
  808. X    rawmode(TRUE);
  809. X    redraw(TRUE);
  810. X    moveto(17, 0);
  811. X    printf("cmd:%d> ", cnt);
  812. X    cleol();
  813. X    printf("%s", resgb);
  814. X    fflush(stdout);
  815. X }
  816. X
  817. Xsetsig(on)
  818. X {
  819. X    signal(SIGINT, on ? SIG_IGN : SIG_DFL);
  820. X    signal(SIGQUIT, on ? SIG_IGN : SIG_DFL);
  821. X    signal(SIGTERM, on ? SIG_IGN : SIG_DFL);
  822. X    signal(SIGTSTP, on ? SIG_IGN : SIG_DFL);
  823. X    signal(SIGCONT, on ? restart : SIG_DFL);
  824. X }
  825. X
  826. Xstop()
  827. X {
  828. X    setsig(FALSE);
  829. X    rawmode(FALSE);
  830. X    signal(SIGCONT, restart);
  831. X    kill(getpid(), SIGTSTP);
  832. X }
  833. X
  834. Xgetcx()
  835. X {
  836. X    char ch;
  837. X
  838. X    signal(SIGTSTP, stop);
  839. X    read(0, &ch, 1);
  840. X    signal(SIGTSTP, SIG_IGN);
  841. X    return(ch);
  842. X }
  843. X
  844. Xrawmode(on)
  845. X {
  846. X    struct sgttyb s;
  847. X    static struct sgttyb saveterm;
  848. X    static int saved = FALSE;
  849. X
  850. X    if (on)
  851. X     {
  852. X    ioctl(0, TIOCGETP, &s);
  853. X    if (saved == FALSE)
  854. X     {
  855. X        saveterm = s;
  856. X        saved = TRUE;
  857. X     }
  858. X    ospeed = s.sg_ospeed;
  859. X    s.sg_flags |= CBREAK;
  860. X    s.sg_flags &= ~(ECHO|XTABS);
  861. X     }
  862. X    else
  863. X      s = saveterm;
  864. X    ioctl(0, TIOCSETN, &s);
  865. X }
  866. X
  867. Xgetterm()
  868. X {
  869. X    char termbuf[1024];
  870. X    char *sp;
  871. X    static char sbuf[150];
  872. X    char *tgetstr();
  873. X
  874. X    tgetent(termbuf, getenv("TERM"));
  875. X    sp = sbuf;
  876. X
  877. X    PC = *(tgetstr("pc", &sp));
  878. X    *UP = *(tgetstr("pc", &sp));
  879. X    UP[1] = 0;
  880. X    *BC = (sc_bs = tgetstr("bs", &sp)) ? *sc_bs : '\b';
  881. X    BC[1] = 0;
  882. X    sc_init = tgetstr("ti", &sp);
  883. X    sc_deinit = tgetstr("te", &sp);
  884. X    sc_cleol = tgetstr("ce", &sp);
  885. X    sc_clear = tgetstr("cl", &sp);
  886. X    sc_move = tgetstr("cm", &sp);
  887. X    sc_s_in = tgetstr("so", &sp);
  888. X    sc_s_out = tgetstr("se", &sp);
  889. X }
  890. X
  891. Xxputc(c)
  892. Xregister c;
  893. X {
  894. X    putchar(c);
  895. X }
  896. X
  897. Xinit()
  898. X {
  899. X    tputs(sc_init, 24, xputc);
  900. X }
  901. X
  902. Xdeinit()
  903. X {
  904. X    tputs(sc_deinit, 24, xputc);
  905. X }
  906. X
  907. Xclear()
  908. X {
  909. X    tputs(sc_clear, 24, xputc);
  910. X }
  911. X
  912. Xcleol()
  913. X {
  914. X    tputs(sc_cleol, 1, xputc);
  915. X }
  916. X
  917. Xstandout()
  918. X {
  919. X    tputs(sc_s_in, 1, xputc);
  920. X }
  921. X
  922. Xstandend()
  923. X {
  924. X    tputs(sc_s_out, 1, xputc);
  925. X }
  926. X
  927. Xmoveto(y, x)
  928. X {
  929. X    char *tgoto();
  930. X
  931. X    tputs(tgoto(sc_move, x, y), 1, xputc);
  932. X }
  933. X
  934. Xput(dest, item)
  935. Xint *dest;
  936. X {
  937. X    if (item == -1)
  938. X      return;
  939. X    while (*dest != -1)
  940. X      dest++;
  941. X    *dest++ = item;
  942. X    *dest = -1;
  943. X }
  944. X
  945. Xget(source)
  946. Xint *source;
  947. X {
  948. X    int i;
  949. X
  950. X    for (i = 0; *source != -1; i++, source++)
  951. X      ;
  952. X    if (i)
  953. X     {
  954. X    i = *--source;
  955. X    *source = -1;
  956. X    return(i);
  957. X     }
  958. X    return(-1);
  959. X }
  960. X
  961. Xpeek(source)
  962. Xint *source;
  963. X {
  964. X    int i;
  965. X
  966. X    for (i = 0; source[i] != -1; i++)
  967. X      ;
  968. X    return(i ? source[i - 1] : -1);
  969. X }
  970. END_OF_sol.c
  971. if test 16609 -ne `wc -c <sol.c`; then
  972.     echo shar: \"sol.c\" unpacked with wrong size!
  973. fi
  974. # end of overwriting check
  975. fi
  976. if test -f solx.c -a "${1}" != "-c" ; then 
  977.   echo shar: Will not over-write existing file \"solx.c\"
  978. else
  979. echo shar: Extracting \"solx.c\" \(14772 characters\)
  980. sed "s/^X//" >solx.c <<'END_OF_solx.c'
  981. X#include    <stdio.h>
  982. X#include    <ctype.h>
  983. X#include    <signal.h>
  984. X#include    <sys/time.h>
  985. X#include    <sys/types.h>
  986. X#include    <sys/stat.h>
  987. X#include    <sys/ioctl.h>
  988. X
  989. X#define     TRUE    1
  990. X#define        FALSE    0
  991. X
  992. Xint    amode;
  993. Xint    cnt;
  994. Xint    cheat;
  995. Xint    deck[53];
  996. Xint    ace[4][14];
  997. Xint    run[7][53];
  998. Xint    hidden[7][8];
  999. Xchar    cmd[40], *cp;
  1000. Xchar    *resgb;
  1001. X
  1002. Xchar    PC;
  1003. Xchar    BC[2];
  1004. Xchar    UP[2];
  1005. X
  1006. Xchar    *sc_bs;
  1007. Xchar    *sc_move;
  1008. Xchar    *sc_clear;
  1009. Xchar    *sc_cleol;
  1010. Xchar    *sc_init;
  1011. Xchar    *sc_deinit;
  1012. Xchar    *sc_s_in;
  1013. Xchar    *sc_s_out;
  1014. X
  1015. Xshort    ospeed;
  1016. X
  1017. Xint    long_run[7];
  1018. Xint    long_hide[7];
  1019. Xint    long_ace[4];
  1020. Xint    base_run[7];
  1021. X
  1022. Xmain(n, a)
  1023. Xchar **a;
  1024. X {
  1025. X    int i, p, c, r, s;
  1026. X    struct timeval tp;
  1027. X    struct timezone tzp;
  1028. X    int from;
  1029. X    int split;
  1030. X    int dest;
  1031. X    int brkflg;
  1032. X
  1033. X    amode = 0;
  1034. X    gettimeofday(&tp, &tzp);
  1035. X    srand(getpid() + tp.tv_sec);
  1036. X    rawmode(TRUE);
  1037. X    setsig(TRUE);
  1038. X    getterm();
  1039. X    init();
  1040. X    for (i = 0; i < 52; i++)
  1041. X      deck[i] = i;
  1042. X    deck[52] = -1;
  1043. X    shuffle(deck);
  1044. X    for (i = 0; i < 4; i++)
  1045. X      ace[i][0] = -1;
  1046. X    for (i = 0; i < 7; i++)
  1047. X      hidden[i][0] = run[i][0] = -1;
  1048. X    for (p = 0; p < 7; p++)
  1049. X      for (c = p; c < 7; c++)
  1050. X    put(hidden[c],get(deck));
  1051. X    put(run[0], get(hidden[0]));
  1052. X    for (r = 1; r < 7; r++)
  1053. X      for (i = 0; i < 4; i++)
  1054. X        put(run[r],get(deck));
  1055. X    redraw(TRUE);
  1056. X    for (r = 0; r < 7; r++)
  1057. X     {
  1058. X    while (getvalue(peek(run[r])) == 0)
  1059. X     {
  1060. X        s = getsuit(peek(run[r]));
  1061. X        put(ace[s],get(run[r]));
  1062. X        if (run[r][0] == -1)
  1063. X          put(run[r],get(hidden[r]));
  1064. X        redraw(FALSE);
  1065. X     }
  1066. X     }
  1067. X    cnt = cheat = 0;
  1068. X    if (n > 1 && !strcmp(a[1], "auto"))
  1069. X      autopilot();
  1070. X    brkflg = 1;
  1071. X    while (brkflg)
  1072. X     {
  1073. X    cnt++;
  1074. X    do
  1075. X     {
  1076. X        moveto(19, 0);
  1077. X        printf("cmd:%d> ", cnt);
  1078. X        cleol();
  1079. X        fflush(stdout);
  1080. X     } while (!getst(cp = cmd));
  1081. X    while (*cp == ' ' || *cp == '\t')
  1082. X      cp++;
  1083. X    switch (*cp++)
  1084. X     {
  1085. X        case 'a':
  1086. X          autopilot();
  1087. X        break;
  1088. X        case 'm':
  1089. X         {
  1090. X        while (*cp == ' ' || *cp == '\t')
  1091. X          cp++;
  1092. X        if (!(from = *cp++) || from < '1' || from > '7')
  1093. X         {
  1094. X            whoops();
  1095. X            continue;
  1096. X         }
  1097. X        while (*cp == ' ' || *cp == '\t')
  1098. X          cp++;
  1099. X        for (split = 0; isdigit(*cp); split = split * 10 + *cp++ - '0')
  1100. X          ;
  1101. X        if (split < 1 || split > lstlen(run[from - '1']))
  1102. X         {
  1103. X            whoops();
  1104. X            continue;
  1105. X         }
  1106. X        while (*cp == ' ' || *cp == '\t')
  1107. X          cp++;
  1108. X        if (!(dest = *cp++) || ((dest < '1' || dest > '7') &&
  1109. X            dest != 'a') || !movecard(from, split, dest, FALSE))
  1110. X         {
  1111. X            whoops();
  1112. X            continue;
  1113. X         }
  1114. X        if (ordered())
  1115. X          finish();
  1116. X         }
  1117. X        break;
  1118. X        case 'h':
  1119. X        case '?':
  1120. X          disphelp();
  1121. X        break;
  1122. X        case 'r':
  1123. X          disprules();
  1124. X        break;
  1125. X        case 'q':
  1126. X          brkflg = 0;
  1127. X        break;
  1128. X        case 'w':
  1129. X         {
  1130. X        while (*cp == ' ' || *cp == '\t')
  1131. X          cp++;
  1132. X        if ((from = *cp - '1') < 0 || from > 6)
  1133. X         {
  1134. X            whoops();
  1135. X            continue;
  1136. X         }
  1137. X        moveto(21, 0);
  1138. X        printf("%d cards hidden under run %c:\n", lstlen(hidden[from]),
  1139. X                                from + '1');
  1140. X        for (i = 0; hidden[from][i] != -1; i++)
  1141. X         {
  1142. X            pcard(hidden[from][i]);
  1143. X            putchar(' ');
  1144. X         }
  1145. X        printf("\nHit return -");
  1146. X        fflush(stdout);
  1147. X        getcx();
  1148. X        moveto(21, 0);
  1149. X        cleol();
  1150. X        putchar('\n');
  1151. X        cleol();
  1152. X        cheat++;
  1153. X         }
  1154. X        break;
  1155. X        case 'p':
  1156. X         {
  1157. X        while (*cp == ' ' || *cp == '\t')
  1158. X          cp++;
  1159. X        if ((from = *cp - '1') < 0 || from > 6)
  1160. X         {
  1161. X            whoops();
  1162. X            continue;
  1163. X         }
  1164. X        while (hidden[from][0] != -1)
  1165. X          put(run[from], get(hidden[from]));
  1166. X        redraw(FALSE);
  1167. X        cheat++;
  1168. X         }
  1169. X        break;
  1170. X        case 's':
  1171. X         {
  1172. X        while (*cp == ' ' || *cp == '\t')
  1173. X          cp++;
  1174. X        if ((from = *cp - '1') < 0 || from > 6)
  1175. X         {
  1176. X            whoops();
  1177. X            continue;
  1178. X         }
  1179. X        shuffle(run[from]);
  1180. X        long_run[from] = -1;
  1181. X        redraw(FALSE);
  1182. X        cheat++;
  1183. X         }
  1184. X        break;
  1185. X        default:
  1186. X          whoops();
  1187. X        break;
  1188. X     }
  1189. X     }
  1190. X    moveto(21, 0);
  1191. X    cleol();
  1192. X    printf("I see you gave up\n");
  1193. X    if (cheat > 0)
  1194. X      printf("    ... even after you cheated %d times!\n", cheat);
  1195. X    else
  1196. X      printf("    ... but at least you didn't cheat...congratulations!\n");
  1197. X    unwind();
  1198. X }
  1199. X
  1200. Xunwind()
  1201. X {
  1202. X    moveto(23, 0);
  1203. X    deinit();
  1204. X    rawmode(FALSE);
  1205. X    exit(0);
  1206. X }
  1207. X
  1208. Xmovecard(from, split, dest, limitmove)
  1209. X {
  1210. X    if (from == dest)
  1211. X      return(FALSE);
  1212. X    return(dest == 'a' ? run2ace(from) : run2run(from, split, dest, limitmove));
  1213. X }
  1214. X
  1215. Xrun2ace(from)
  1216. X {
  1217. X    int fcard, s, ok;
  1218. X
  1219. X    fcard = peek(run[from -= '1']);
  1220. X    if (ok = getvalue(peek(ace[s = getsuit(fcard)])) == getvalue(fcard) - 1)
  1221. X     {
  1222. X    put(ace[s], get(run[from]));
  1223. X    redraw(FALSE);
  1224. X    tidy(from);
  1225. X     }
  1226. X    return(ok);
  1227. X }
  1228. X
  1229. Xrun2run(from, split, dest, limitmove)
  1230. X {
  1231. X    int fcard, dcard, i, ok, s;
  1232. X
  1233. X    if ((fcard = run[from -= '1'][--split]) == -1)
  1234. X      return(FALSE);
  1235. X    dcard = peek(run[dest -= '1']);
  1236. X    if (amode > 0 && dcard == -1 && getvalue(fcard) == 12 &&
  1237. X                        lstlen(hidden[from]) == 0)
  1238. X      return(FALSE);
  1239. X    if (amode > 0 && limitmove && split && chk2run(fcard, run[from][split - 1]))
  1240. X      return(FALSE);
  1241. X    if (ok = chk2run(fcard, dcard))
  1242. X     {
  1243. X    for (i = split; run[from][i] != -1; i++)
  1244. X      put(run[dest], run[from][i]);
  1245. X    run[from][split] = -1;
  1246. X    redraw(FALSE);
  1247. X    tidy(from);
  1248. X     }
  1249. X    return(ok);
  1250. X }
  1251. X
  1252. Xtidy(from)
  1253. X {
  1254. X    int s;
  1255. X
  1256. X    while (getvalue(peek(run[from])) == 0)
  1257. X     {
  1258. X    s = getsuit(peek(run[from]));
  1259. X    put(ace[s],get(run[from]));
  1260. X    redraw(FALSE);
  1261. X     }
  1262. X    if (lstlen(hidden[from]) != 0 && lstlen(run[from]) == 0)
  1263. X     {
  1264. X    while (getvalue(peek(hidden[from])) == 0)
  1265. X     {
  1266. X        s = getsuit(peek(hidden[from]));
  1267. X        put(ace[s],get(hidden[from]));
  1268. X        redraw(FALSE);
  1269. X     }
  1270. X    put(run[from],get(hidden[from]));
  1271. X    redraw(FALSE);
  1272. X     }
  1273. X }
  1274. X
  1275. Xchk2run(fcard,dcard)
  1276. X {
  1277. X    if (dcard == -1 && getvalue(fcard) == 12)
  1278. X      return(TRUE);
  1279. X    return(getvalue(dcard) - 1 == getvalue(fcard) &&
  1280. X                getcolour(dcard) != getcolour(fcard));
  1281. X }
  1282. X
  1283. Xordered()
  1284. X {
  1285. X    int i, j;
  1286. X
  1287. X    for (i = 0; i < 7; i++)
  1288. X      if (hidden[i][0] != -1)
  1289. X    return(FALSE);
  1290. X      else if (run[i][0] != -1)
  1291. X    for (j = 1; run[i][j] != -1; j++)
  1292. X      if (getvalue(run[i][j]) != getvalue(run[i][j - 1]) - 1 ||
  1293. X            getcolour(run[i][j]) == getcolour(run[i][j - 1]))
  1294. X        return(FALSE);
  1295. X    return(TRUE);
  1296. X }
  1297. X
  1298. Xcardsleft()
  1299. X {
  1300. X    int i;
  1301. X    for (i = 0; i < 7; i++)
  1302. X      if (run[i][0] != -1)
  1303. X    return(TRUE);
  1304. X    return(FALSE);
  1305. X }
  1306. X
  1307. Xfinish()
  1308. X {
  1309. X    int i;
  1310. X
  1311. X    moveto(21, 0);
  1312. X    printf("\007I'll finish for you now\007");
  1313. X    fflush(stdout);
  1314. X    do
  1315. X      for (i = 0; i < 7; i++)
  1316. X    while (movecard(i + '1', 0, 'a', FALSE))
  1317. X      ;
  1318. X     while (cardsleft());
  1319. X    moveto(21, 40);
  1320. X    cleol();
  1321. X    printf("\007You WIN\007\n");
  1322. X    if (cheat > 0)
  1323. X      printf("    ... but you cheated %d times!", cheat);
  1324. X    else
  1325. X      printf("    ... and without cheating ... congratulations!");
  1326. X    unwind();
  1327. X }
  1328. X
  1329. Xautopilot()
  1330. X {
  1331. X    int i, j, k, l;
  1332. X    int totdec;
  1333. X    int iter;
  1334. X
  1335. X    moveto(21, 0);
  1336. X    printf("Going into automatic mode...");
  1337. X    fflush(stdout);
  1338. X    amode = TRUE;
  1339. X    do
  1340. X     {
  1341. X    l = 0;
  1342. X    for (i = 0; i < 7; i++)
  1343. X      for (j = 0; j < 7; j++)
  1344. X        for (k = 0; k < lstlen(run[i]); k++)
  1345. X          l = movecard(i + '1', k, j + '1', TRUE) || l;
  1346. X    for (i = 0; i < 7; i++)
  1347. X      l = movecard(i + '1', 0, 'a', FALSE) || l;
  1348. X     } while (l);
  1349. X    moveto(21, 28);
  1350. X    if (!cardsleft())
  1351. X     {
  1352. X    printf("\007YEA...\007");
  1353. X    fflush(stdout);
  1354. X    for (i = 0; i < 7; i++)
  1355. X      if (movecard(i + '1', 0, 'a', FALSE))
  1356. X    moveto(21, 34);
  1357. X    printf("I won!!!!!");
  1358. X     }
  1359. X    else
  1360. X      printf("I couldn't win this time");
  1361. X    unwind();
  1362. X }
  1363. X
  1364. Xredraw(display)
  1365. X {
  1366. X    int i, j, k;
  1367. X
  1368. X    if (display)
  1369. X     {
  1370. X    clear();
  1371. X    for (i = 0; i < 7; i++)
  1372. X     {
  1373. X        long_run[i] = long_hide[i] = base_run[i] = -1;
  1374. X        moveto(i + i + 2, 1);
  1375. X        putchar(i + '1');
  1376. X     }
  1377. X    moveto(0, 6);
  1378. X    for (i = 1; i < 21; i++)
  1379. X      printf("%3d", i);
  1380. X    for (i = 0; i < 4; i++)
  1381. X      long_ace[i] = -1;
  1382. X    moveto(17, 1);
  1383. X    printf("ACES:");
  1384. X     }
  1385. X    for (i = 0; i < 7; i++)
  1386. X     {
  1387. X    if (long_hide[i] != lstlen(hidden[i]))
  1388. X     {
  1389. X        moveto(i + i + 2, 4);
  1390. X        putchar(lstlen(hidden[i]) ? lstlen(hidden[i]) + '0' : ' ');
  1391. X        long_hide[i] = lstlen(hidden[i]);
  1392. X     }
  1393. X     }
  1394. X    for (i = 0; i < 4; i++)
  1395. X     {
  1396. X    if (long_ace[i] != lstlen(ace[i]))
  1397. X     {
  1398. X        moveto(17, 8 + i * 5);
  1399. X        if (ace[i][0] != -1)
  1400. X          pcard(peek(ace[i]));
  1401. X        else
  1402. X          printf("--");
  1403. X        long_ace[i] = lstlen(ace[i]);
  1404. X     }
  1405. X     }
  1406. X    for (i = 0; i < 7; i++)
  1407. X     {
  1408. X    if (long_run[i] != lstlen(run[i]) ||
  1409. X                (long_run[i] == 1 && base_run[i] != run[i][0]))
  1410. X     {
  1411. X        moveto(i + i +  2, 7);
  1412. X        for (j = 0; run[i][j] != -1; j++)
  1413. X         {
  1414. X        if (run[i][j] == -1)
  1415. X          printf("  ");
  1416. X        else
  1417. X          pcard(run[i][j]);
  1418. X        putchar(' ');
  1419. X         }
  1420. X        while (j++ < long_run[i])
  1421. X          printf("   ");
  1422. X        long_run[i] = lstlen(run[i]);
  1423. X        base_run[i] = run[i][0];
  1424. X     }
  1425. X     }
  1426. X    fflush(stdout);
  1427. X    sleep(1);
  1428. X }
  1429. X
  1430. Xpcard(card)
  1431. X {
  1432. X    int i;
  1433. X
  1434. X    if (i = getcolour(card))
  1435. X      standout();
  1436. X    printf("%c%c", "A23456789TJQK"[getvalue(card)], "HDSC"[getsuit(card)]);
  1437. X    if (i)
  1438. X      standend();
  1439. X }
  1440. X
  1441. Xgetvalue(card)
  1442. X {
  1443. X    return(card % 13);
  1444. X }
  1445. X
  1446. Xgetsuit(card)
  1447. X {
  1448. X    return(card / 13);
  1449. X }
  1450. X
  1451. Xgetcolour(card)
  1452. X {
  1453. X    return(card / 26);
  1454. X }
  1455. X
  1456. Xwhoops()
  1457. X {
  1458. X    moveto(19, 0);
  1459. X    printf("\007Invalid Command: '%s'\007", cmd);
  1460. X    fflush(stdout);
  1461. X    sleep(4);
  1462. X }
  1463. X
  1464. Xdisphelp()
  1465. X {
  1466. X    clear();
  1467. X    printf("\
  1468. XCommands: m [1-7] [pos] [1-7a] : move cards or runs\n\
  1469. X          a                    : turn on the auto pilot\n\
  1470. X          s [1-7]              : shuffle the cards in a run (cheat!)\n\
  1471. X          p [1-7]              : put a hidden pile onto the run (cheat!)\n\
  1472. X          w [1-7]              : print the cards in a hidden pile (cheat!)\n\
  1473. X          h or ?               : print this command summary\n\
  1474. X          r                    : print the rules of the game\n\
  1475. X          q                    : quit\n\n");
  1476. X    printf("\
  1477. XMoving:   There are three parameters in the m command: the first is the\n\
  1478. X          run to be moved from, the second is the card number to split\n\
  1479. X          the run at (check numbers along the top), and the third is\n\
  1480. X      the destination run (or 'a' if an ace pile). Since only one card\n\
  1481. X      can be moved to an ace at a time, the pos paramter is ignored\n\
  1482. X      in this case. To give a couple of examples 'm 6 1 4 would move\n\
  1483. X      all cards from run 6 to run 4, and m 3 5 7 would split run 3\n\
  1484. X      before the fifth card (leaving 4) and move the rest to run 7.\n\n\
  1485. XCheating: Commands that allow cheating are available but they will count\n\
  1486. X          against you in your next life!\n\n\nHit Return -");
  1487. X    fflush(stdout);
  1488. X    getcx();
  1489. X    redraw(TRUE);
  1490. X }
  1491. X
  1492. Xdisprules()
  1493. X {
  1494. X    clear();
  1495. X    printf("\
  1496. XObject:   The object of this game is to get all of the cards in each suit\n\
  1497. X          in order on the proper ace pile.\n\n\
  1498. XRules:    Cards are played on the ace piles in ascending order: A,2,...,K.\n\
  1499. X          All aces are automatically placed in the correct aces pile as\n\
  1500. X          they're uncovered in runs or from a pile of hidden cards. Once a\n\
  1501. X          card is placed in an ace pile it can't be removed.\n\n");
  1502. X    printf("\
  1503. X          When moving runs, they can be split anywhere, but the top card\n\
  1504. X      in the run moved must be placed on a card one higher (i.e. 5 on\n\
  1505. X      a 6) and always on a card of the opposite color.\n\n");
  1506. X    printf("\
  1507. X          Whenever a whole run is moved, the top hidden card is turned\n\
  1508. X          over, thus becoming the beginning of a new run.  If there are no\n\
  1509. X          hidden cards left, a space is created which can only be filled by\n\
  1510. X          a king.\n\n\nHit Return -");
  1511. X    fflush(stdout);
  1512. X    getcx();
  1513. X    redraw(TRUE);
  1514. X }
  1515. X
  1516. Xgetst(getbuf, pc)
  1517. Xchar *getbuf;
  1518. X {
  1519. X    int ch;
  1520. X    int nc;
  1521. X
  1522. X    *(resgb = getbuf) = nc = 0;
  1523. X    for (;;)
  1524. X     {
  1525. X    if ((((ch = getcx()) >= ' ' && ch <= '~') || ch == '\t') && nc < 40)
  1526. X     {
  1527. X        *getbuf++ = ch;
  1528. X        *getbuf = 0;
  1529. X        nc++;
  1530. X        putchar(ch);
  1531. X        fflush(stdout);
  1532. X     }
  1533. X    else if (ch == '\b')
  1534. X     {
  1535. X        if (nc--)
  1536. X         {
  1537. X            *--getbuf = 0;
  1538. X        printf("\b \b");
  1539. X        fflush(stdout);
  1540. X         }
  1541. X        else
  1542. X          return(FALSE);
  1543. X     }
  1544. X    else if (ch == 'r' & 0x1f)
  1545. X      redraw(TRUE);
  1546. X    else if (ch == '\n')
  1547. X      return(TRUE);
  1548. X     }
  1549. X }
  1550. X
  1551. Xshuffle(cards)
  1552. Xint *cards;
  1553. X {
  1554. X    int i, j, k, t;
  1555. X
  1556. X    for (i = 0; cards[i] != -1; i++)
  1557. X      ;
  1558. X    if (i)
  1559. X     {
  1560. X    for (j = 0; j < i; j++)
  1561. X     {
  1562. X        do
  1563. X          k = rnd(i);
  1564. X         while (k == j);
  1565. X        t = cards[j];
  1566. X        cards[j] = cards[k];
  1567. X        cards[k] = t;
  1568. X     }
  1569. X     }
  1570. X }
  1571. X
  1572. Xrnd(n)
  1573. X {
  1574. X    return(((rand() >> 12 | rand() >> 24) & (unsigned) ~0 >> 1) % n);
  1575. X }
  1576. X
  1577. Xlstlen(list)
  1578. Xint *list;
  1579. X {
  1580. X    int i;
  1581. X
  1582. X    for (i = 0; list[i] != -1; i++)
  1583. X      ;
  1584. X    return(i);
  1585. X }
  1586. X
  1587. Xrestart()
  1588. X {
  1589. X    setsig(TRUE);
  1590. X    rawmode(TRUE);
  1591. X    redraw(TRUE);
  1592. X    moveto(19, 0);
  1593. X    printf("cmd:%d> ", cnt);
  1594. X    cleol();
  1595. X    printf("%s", resgb);
  1596. X    fflush(stdout);
  1597. X }
  1598. X
  1599. Xsetsig(on)
  1600. X {
  1601. X    signal(SIGINT, on ? SIG_IGN : SIG_DFL);
  1602. X    signal(SIGQUIT, on ? SIG_IGN : SIG_DFL);
  1603. X    signal(SIGTERM, on ? SIG_IGN : SIG_DFL);
  1604. X    signal(SIGTSTP, on ? SIG_IGN : SIG_DFL);
  1605. X    signal(SIGCONT, on ? restart : SIG_DFL);
  1606. X }
  1607. X
  1608. Xstop()
  1609. X {
  1610. X    setsig(FALSE);
  1611. X    rawmode(FALSE);
  1612. X    signal(SIGCONT, restart);
  1613. X    kill(getpid(), SIGTSTP);
  1614. X }
  1615. X
  1616. Xgetcx()
  1617. X {
  1618. X    char ch;
  1619. X
  1620. X    signal(SIGTSTP, stop);
  1621. X    read(0, &ch, 1);
  1622. X    signal(SIGTSTP, SIG_IGN);
  1623. X    return(ch);
  1624. X }
  1625. X
  1626. Xrawmode(on)
  1627. X {
  1628. X    struct sgttyb s;
  1629. X    static struct sgttyb saveterm;
  1630. X    static int saved = FALSE;
  1631. X
  1632. X    if (on)
  1633. X     {
  1634. X    ioctl(0, TIOCGETP, &s);
  1635. X    if (saved == FALSE)
  1636. X     {
  1637. X        saveterm = s;
  1638. X        saved = TRUE;
  1639. X     }
  1640. X    ospeed = s.sg_ospeed;
  1641. X    s.sg_flags |= CBREAK;
  1642. X    s.sg_flags &= ~(ECHO|XTABS);
  1643. X     }
  1644. X    else
  1645. X      s = saveterm;
  1646. X    ioctl(0, TIOCSETN, &s);
  1647. X }
  1648. X
  1649. Xgetterm()
  1650. X {
  1651. X    char termbuf[1024];
  1652. X    char *sp;
  1653. X    static char sbuf[150];
  1654. X    char *tgetstr();
  1655. X
  1656. X    tgetent(termbuf, getenv("TERM"));
  1657. X    sp = sbuf;
  1658. X
  1659. X    PC = *(tgetstr("pc", &sp));
  1660. X    *UP = *(tgetstr("pc", &sp));
  1661. X    UP[1] = 0;
  1662. X    *BC = (sc_bs = tgetstr("bs", &sp)) ? *sc_bs : '\b';
  1663. X    BC[1] = 0;
  1664. X    sc_init = tgetstr("ti", &sp);
  1665. X    sc_deinit = tgetstr("te", &sp);
  1666. X    sc_cleol = tgetstr("ce", &sp);
  1667. X    sc_clear = tgetstr("cl", &sp);
  1668. X    sc_move = tgetstr("cm", &sp);
  1669. X    sc_s_in = tgetstr("so", &sp);
  1670. X    sc_s_out = tgetstr("se", &sp);
  1671. X }
  1672. X
  1673. Xxputc(c)
  1674. Xregister c;
  1675. X {
  1676. X    putchar(c);
  1677. X }
  1678. X
  1679. Xinit()
  1680. X {
  1681. X    tputs(sc_init, 24, xputc);
  1682. X }
  1683. X
  1684. Xdeinit()
  1685. X {
  1686. X    tputs(sc_deinit, 24, xputc);
  1687. X }
  1688. X
  1689. Xclear()
  1690. X {
  1691. X    tputs(sc_clear, 24, xputc);
  1692. X }
  1693. X
  1694. Xcleol()
  1695. X {
  1696. X    tputs(sc_cleol, 1, xputc);
  1697. X }
  1698. X
  1699. Xstandout()
  1700. X {
  1701. X    tputs(sc_s_in, 1, xputc);
  1702. X }
  1703. X
  1704. Xstandend()
  1705. X {
  1706. X    tputs(sc_s_out, 1, xputc);
  1707. X }
  1708. X
  1709. Xmoveto(y, x)
  1710. X {
  1711. X    char *tgoto();
  1712. X
  1713. X    tputs(tgoto(sc_move, x, y), 1, xputc);
  1714. X }
  1715. X
  1716. Xput(dest, item)
  1717. Xint *dest;
  1718. X {
  1719. X    if (item == -1)
  1720. X      return;
  1721. X    while (*dest != -1)
  1722. X      dest++;
  1723. X    *dest++ = item;
  1724. X    *dest = -1;
  1725. X }
  1726. X
  1727. Xget(source)
  1728. Xint *source;
  1729. X {
  1730. X    int i;
  1731. X
  1732. X    for (i = 0; *source != -1; i++, source++)
  1733. X      ;
  1734. X    if (i)
  1735. X     {
  1736. X    i = *--source;
  1737. X    *source = -1;
  1738. X    return(i);
  1739. X     }
  1740. X    return(-1);
  1741. X }
  1742. X
  1743. Xpeek(source)
  1744. Xint *source;
  1745. X {
  1746. X    int i;
  1747. X
  1748. X    for (i = 0; source[i] != -1; i++)
  1749. X      ;
  1750. X    return(i ? source[i - 1] : -1);
  1751. X }
  1752. END_OF_solx.c
  1753. if test 14772 -ne `wc -c <solx.c`; then
  1754.     echo shar: \"solx.c\" unpacked with wrong size!
  1755. fi
  1756. # end of overwriting check
  1757. fi
  1758. echo shar: End of shell archive.
  1759. exit 0
  1760.